package org.gd136.ce.script.impl;

import org.gd136.ce.script.Script;
import org.gd136.ce.script.ScriptStage;

public class ElseIfScript extends Script {

	private static final String TAG = "#elseIf ";

	static ElseIfScript lastElseIfScript;

	ElseIfScript nextElseIfScript;
	String checkParam;

	@Override
	public Script readScript(String scriptLine) {
		ElseIfScript eis = null;
		if (scriptLine.startsWith(TAG)) {
			eis = new ElseIfScript();
			eis.checkParam = scriptLine.substring(TAG.length());
			if (lastElseIfScript != null) {
				lastElseIfScript.nextElseIfScript = eis;
			} else if (IfScript.lastIfScript != null) {
				IfScript.lastIfScript.nextIfScript = eis;
			} else {
				throw new IllegalArgumentException("elseIf 没有处于 If内! file["
						+ fileName + "] line[" + lineNum + "]");
			}
			lastElseIfScript = eis;
			eis.addToPool();
		}
		return eis;
	}

	@Override
	public void execute(ScriptStage stage) {
		if (!stage.ifCheck(checkParam)) {
			SM.stack.remove(SM.stack.size() - 1);
			SM.stack.add(nextElseIfScript);
		}
	}

}
